A Wrapper class is a class whose object wraps a primitive data type.
Java provides these classes in the java.lang
package to convert primitive types into
objects.
Key Difference: Primitives are not objects, stored on stack, faster; Wrappers are objects, stored on heap, can be null, have methods.
ArrayList
, HashMap
can only
store objects (not primitives)parseInt()
,
compare()
, valueOf()
<T>
) do not support
primitives, only objectsAutoboxing = primitive โก๏ธ object (like putting primitive in a box)
Unboxing = object โก๏ธ primitive (opening the box)
public class AutoBoxingExample { public static void main(String[] args) { int a = 10; // Autoboxing Integer obj = a; // Unboxing int b = obj; System.out.println("Object: " + obj); System.out.println("Primitive: " + b); } }
Note: Introduced in Java 5 for seamless conversion. But beware of NullPointerException during unboxing if wrapper is null.
Let's say you want to store user ages in a list:
ArrayList<Integer> ages = new ArrayList<>(); ages.add(25); // Autoboxing happens here: int โ Integer ages.add(30); int sum = ages.get(0) + ages.get(1); // Unboxing: Integer โ int System.out.println("Sum of ages: " + sum);
Analogy: Primitives are like raw ingredients; wrappers are packaged versions for storage in a fridge (collections).
public class WrapperMethods { public static void main(String[] args) { String number = "100"; int value = Integer.parseInt(number); // Convert string to int System.out.println("Parsed value: " + value); String text = Integer.toString(1234); // Convert int to string System.out.println("String value: " + text); boolean result = Boolean.parseBoolean("true"); System.out.println("Boolean value: " + result); char ch = 'A'; System.out.println("Is Digit: " + Character.isDigit(ch)); System.out.println("Is Uppercase: " + Character.isUpperCase(ch)); } }
Key Point: All wrappers are immutable โ once created, value can't change.
Test your understanding with these expandable questions. Click to reveal answers.
Integer
Primitive to wrapper object conversion automatically.
Yes, unlike primitives.
To convert String to int.
๐น Question | ๐ฌ Expected Answer |
---|---|
What is a wrapper class? | A class that converts a primitive type into an object. |
Why are wrapper classes needed? | For collections, generics, null handling, utilities. |
Difference between Integer and int? | int is primitive, Integer is a class (object). |
What is autoboxing/unboxing? | Auto conversion between primitive and wrapper. |
Can you store int in ArrayList? |
Not directly โ it uses Integer via autoboxing. |
Are wrapper classes immutable? | Yes, their values cannot be changed after creation. |
What is the risk with unboxing? | NullPointerException if wrapper is null. |
Difference between valueOf() and parseInt()? | valueOf() returns Integer, parseInt() returns int. |
Generics allows writing code that works with different data types while providing type safety.
Introduced in: Java 5
class Box<T> { T value; void setValue(T val) { value = val; } T getValue() { return value; } }
Box<String> s = new Box<>(); s.setValue("Hi"); System.out.println(s.getValue());
public <T> void print(T[] arr) { for (T t : arr) System.out.println(t); }
class Demo<T extends Number> { void show(T n) { System.out.println(n.doubleValue()); } }
<?>
โ unknown type<? extends Number>
โ any subclass of Number<? super Integer>
โ any superclass of IntegerAnalogy: Wildcards are like jokers in cards โ flexible but with rules.
Test your understanding with these expandable questions. Click to reveal answers.
Type safety in collections without casting.
Upper bounded wildcard โ subtypes of T.
No, only reference types; use wrappers for primitives.
๐น Question | ๐ฌ Expected Answer |
---|---|
What are generics in Java? | Parameterized types for type-safe code. |
Benefits of generics? | Type safety, no casting, compile-time checks. |
What is erasure? | Generics info removed at compile-time for backward compatibility. |
Difference between List> and List | List> is raw wildcard, can't add; List |
What are bounded generics? | Restrict types, e.g., |
PECS rule? | Producer Extends, Consumer Super for wildcards. |
A fully object-oriented language is one where:
Smalltalk
, Ruby
, Scala
Reason | Explanation |
---|---|
Primitive Types | Java has int , float , char , etc., which are not
objects. (For performance reasons) |
Static Methods/Variables | Methods like Math.sqrt() don't need objects. |
Limited Operator Overloading | Custom operator overloading is not allowed. |
Top-Level Code Outside Objects | Static context used before object creation (e.g., main method). |
Why Primitives? Faster execution, less memory โ primitives on stack, objects on heap.
Test your understanding with these expandable questions. Click to reveal answers.
For better performance and memory efficiency.
No, as it doesn't require an object instance.
Smalltalk or Ruby.
๐น Question | ๐ฌ Expected Answer |
---|---|
Is Java fully object-oriented? | No, because it uses primitive types and static members. |
Why does Java have primitives? | For performance โ primitives are faster and use less memory. |
What makes Java object-oriented? | Class-based, supports encapsulation, inheritance, polymorphism, and abstraction. |
What breaks pure OOP in Java? | int , boolean , static methods/fields, etc. |
Can Java be considered OOP? | Yes, it's OOP but not purely due to non-object elements. |
Advantages of not being pure OOP? | Better performance with primitives, easier static utilities. |
int
, char
, etc.)Integer
, Double
, etc.